package com.zhiche.lisa.bms.service.utils;

import com.alibaba.fastjson.JSONObject;
import com.zhiche.lisa.bms.dao.model.interfacelog.InterfaceLog;
import com.zhiche.lisa.bms.enums.InterfaceLogTypeEnum;
import com.zhiche.lisa.bms.service.interfacelog.InterfaceLogService;
import com.zhiche.lisa.core.enums.CommonEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author linbao
 * @date 2018-11-07
 */
@Component
public class InterfaceLogUtil {

    @Autowired
    private InterfaceLogService interfaceLogService;

    /**
     * 保存正确的信息
     *
     * @param interfaceLog
     */
    public void setSuccessLog(InterfaceLog interfaceLog) {
        interfaceLog.setResultCode(String.valueOf(CommonEnum.SUCCESS.getCode()));
        interfaceLog.setResultMessage(CommonEnum.SUCCESS.getText());
    }

    public void setCallPartySuccessLog(InterfaceLog interfaceLog, Object obj) {
        if (obj instanceof String) {
            interfaceLog.setResultData(obj.toString());
        } else {
            interfaceLog.setResultData(JSONObject.toJSONString(obj));
        }
    }

    /**
     * 保存错误的信息
     *
     * @param interfaceLog
     * @param errorMsg
     */
    public void setErrorLog(InterfaceLog interfaceLog, String errorMsg) {
        interfaceLog.setResultCode(String.valueOf(CommonEnum.ERROR.getCode()));
        if (StringUtils.isNotBlank(errorMsg) && errorMsg.length() > 500) {
            errorMsg = errorMsg.substring(0, 500);
        }
        interfaceLog.setResultMessage(errorMsg);
    }

    /**
     * 被调用方
     *
     * @param interfaceLog
     * @param obj
     * @param url
     */
    public void setCalleeCommonLog(InterfaceLog interfaceLog, Object obj, String url) {
        interfaceLog.setType(InterfaceLogTypeEnum.CALLEE.getCode());
        setCommonLog(interfaceLog, obj, url);
    }

    /**
     * 调用方
     *
     * @param interfaceLog
     * @param obj
     * @param url
     */
    public void setCallerPartyCommonLog(InterfaceLog interfaceLog, Object obj, String url) {
        interfaceLog.setType(InterfaceLogTypeEnum.CALLER_PARTY.getCode());
        setCommonLog(interfaceLog, obj, url);
    }

    /**
     * 保存通用信息
     *
     * @param interfaceLog
     * @param obj
     * @param url
     */
    private void setCommonLog(InterfaceLog interfaceLog, Object obj, String url) {
        interfaceLog.setGmtCreate(new Date());
        interfaceLog.setUrl(url);
        if (obj instanceof String) {
            interfaceLog.setParam(obj.toString());
        } else {
            interfaceLog.setParam(JSONObject.toJSONString(obj));
        }
    }

    /**
     * 保存日志
     *
     * @param interfaceLog
     */
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.NOT_SUPPORTED)
    public void saveInterfaceLog(InterfaceLog interfaceLog) {
        interfaceLogService.insert(interfaceLog);
    }
}
